﻿using AdminManagement.Config;
using AdminManagement.Dept;
using AdminManagement.Dict;
using AdminManagement.Menu;
using AdminManagement.Notice;
using AdminManagement.Role;
using AdminManagement.Rooms;
using AdminManagement.User;
using AdminManagement.Utility;
using Microsoft.EntityFrameworkCore;
using System;
using Volo.Abp.AuditLogging.EntityFrameworkCore;
using Volo.Abp.Data;
using Volo.Abp.EntityFrameworkCore;
using Volo.Abp.EntityFrameworkCore.Modeling;

namespace AdminManagement.EntityFrameworkCore;

[ConnectionStringName("Default")]
public class AdminManagementDbContext :
    AbpDbContext<AdminManagementDbContext>
{
    /* Add DbSet properties for your Aggregate Roots / Entities here. */
    public DbSet<SysDictData> SysDictDatas { get; set; }
    public DbSet<SysDict> SysDicts { get; set; }
    public DbSet<SysDept> SysDepts { get; set; }
    public DbSet<SysUser> SysUsers { get; set; }
    public DbSet<SysRole> SysRoles { get; set; }
    public DbSet<SysUserRole> SysUserRoles { get; set; }
    public DbSet<SysMenu> SysMenus { get; set; }
    public DbSet<SysRoleMenu> SysRoleMenus { get; set; }
    public DbSet<SysConfig> SysConfigs { get; set; }
    public DbSet<SysNotice> SysNotices { get; set; }
    public DbSet<SysUserNotice> SysUserNotices { get; set; }

    public DbSet<Room> Rooms { get; set; }



    public AdminManagementDbContext(DbContextOptions<AdminManagementDbContext> options)
        : base(options)
    {

    }

    protected override void OnModelCreating(ModelBuilder builder)
    {
        base.OnModelCreating(builder);

        /* Include modules to your migration db context */

        builder.ConfigureAuditLogging();

        /* Configure your own tables/entities inside here */

        builder.Entity<SysDictData>(b =>
        {
            b.ToTable(nameof(SysDictData).GenerateTableName(), AdminManagementConsts.DbSchema);
            b.Property(m => m.DictCode).HasMaxLength(50).IsRequired();
            b.Property(m => m.Value).HasMaxLength(50).IsRequired();
            b.Property(m => m.Label).HasMaxLength(100).IsRequired();
            b.Property(m => m.TagType).HasMaxLength(50).IsRequired();
            b.Property(m => m.Status).IsRequired();
            b.Property(m => m.Sort).IsRequired();
            b.ConfigureByConvention(); 
        });

        builder.Entity<SysDict>(b =>
        {
            b.ToTable(nameof(SysDict).GenerateTableName(), AdminManagementConsts.DbSchema);
            b.Property(m => m.DictCode).HasMaxLength(50).IsRequired();
            b.Property(m => m.Name).HasMaxLength(50).IsRequired();
            b.Property(m => m.Remark).HasMaxLength(255);
            b.Property(m => m.Status).IsRequired();
            b.ConfigureByConvention();
        });

        builder.Entity<SysDept>(b =>
        {
            b.ToTable(nameof(SysDept).GenerateTableName(), AdminManagementConsts.DbSchema);
            b.Property(m => m.Name).HasMaxLength(100).IsRequired();
            b.Property(m => m.Code).HasMaxLength(100).IsRequired();
            b.Property(m => m.ParentId).IsRequired();
            b.Property(m => m.TreePath).HasMaxLength(255).IsRequired();
            b.Property(m => m.Sort);
            b.Property(m => m.Status).IsRequired();
            b.ConfigureByConvention();
        });

        builder.Entity<SysUser>(b =>
        {
            b.ToTable(nameof(SysUser).GenerateTableName(), AdminManagementConsts.DbSchema);
            b.Property(m => m.Username).HasMaxLength(64).IsRequired();
            b.Property(m => m.Nickname).HasMaxLength(64).IsRequired();
            b.Property(m => m.Gender).IsRequired();
            b.Property(m => m.Password).HasMaxLength(100).IsRequired();
            b.Property(m => m.DeptId).IsRequired();
            b.Property(m => m.Avatar).HasMaxLength(255).IsRequired();
            b.Property(m => m.Mobile).HasMaxLength(20);
            b.Property(m => m.Status).IsRequired();
            b.Property(m => m.Email).HasMaxLength(128);
            b.ConfigureByConvention();
        });

        builder.Entity<SysRole>(b =>
        {
            b.ToTable(nameof(SysRole).GenerateTableName(), AdminManagementConsts.DbSchema);
            b.Property(m => m.Name).HasMaxLength(64).IsRequired();
            b.Property(m => m.Code).HasMaxLength(32).IsRequired();
            b.Property(m => m.Sort).IsRequired();
            b.ConfigureByConvention();
        });

        builder.Entity<SysUserRole>(b =>
        {
            b.ToTable(nameof(SysUserRole).GenerateTableName(), AdminManagementConsts.DbSchema);
            b.HasOne<SysUser>().WithMany().HasForeignKey(m => m.UserId);
            b.HasOne<SysRole>().WithMany().HasForeignKey(m => m.RoleId);
            b.ConfigureByConvention();
        });

        builder.Entity<SysMenu>(b =>
        {
            b.ToTable(nameof(SysMenu).GenerateTableName(), AdminManagementConsts.DbSchema);
            b.Property(m => m.ParentId).IsRequired();
            b.Property(m => m.TreePath).HasMaxLength(255);
            b.Property(m => m.Name).HasMaxLength(64).IsRequired();
            b.Property(m => m.Type).IsRequired();
            b.Property(m => m.RouteName).HasMaxLength(255);
            b.Property(m => m.RoutePath).HasMaxLength(128);
            b.Property(m => m.Component).HasMaxLength(128);
            b.Property(m => m.Perm).HasMaxLength(128);
            b.Property(m => m.Visible).IsRequired();
            b.Property(m => m.Icon).HasMaxLength(64);
            b.Property(m => m.Redirect).HasMaxLength(128);
            b.ConfigureByConvention();
        });

        builder.Entity<SysRoleMenu>(b =>
        {
            b.ToTable(nameof(SysRoleMenu).GenerateTableName(), AdminManagementConsts.DbSchema);
            b.HasOne<SysRole>().WithMany().HasForeignKey(m => m.RoleId);
            b.HasOne<SysMenu>().WithMany().HasForeignKey(m => m.MenuId);
            b.ConfigureByConvention();
        });

        builder.Entity<SysConfig>(b =>
        {
            b.ToTable(nameof(SysConfig).GenerateTableName(), AdminManagementConsts.DbSchema);
            b.Property(m => m.ConfigName).HasMaxLength(50);
            b.Property(m => m.ConfigKey).HasMaxLength(50);
            b.Property(m => m.ConfigValue).HasMaxLength(100);
            b.Property(m => m.Remark).HasMaxLength(200);
            b.ConfigureByConvention();
        });

        builder.Entity<SysNotice>(b =>
        {
            b.ToTable(nameof(SysNotice).GenerateTableName(), AdminManagementConsts.DbSchema);
            b.Property(m => m.Title).HasMaxLength(50).IsRequired();
            b.Property(m => m.Content).IsRequired();
            b.Property(m => m.TargetUserIds).HasMaxLength(255);
            b.ConfigureByConvention();
        });

        builder.Entity<SysUserNotice>(b =>
        {
            b.ToTable(nameof(SysUserNotice).GenerateTableName(), AdminManagementConsts.DbSchema);
            b.HasOne<SysNotice>().WithMany().HasForeignKey(m => m.NoticeId);
            b.HasOne<SysUser>().WithMany().HasForeignKey(m => m.UserId);
            b.ConfigureByConvention();
        });

        builder.Entity<Room>(b =>
        {
            b.ToTable(nameof(Room).GenerateTableName(), AdminManagementConsts.DbSchema);
            b.Property(m => m.RoomName).HasMaxLength(50).IsRequired();
            b.Property(m => m.RoomCode).HasMaxLength(50).IsRequired();
            b.ConfigureByConvention();
        });
    }

    protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
    {
        optionsBuilder.LogTo(Console.WriteLine, new[] { Microsoft.EntityFrameworkCore.Diagnostics.RelationalEventId.CommandExecuting });

        base.OnConfiguring(optionsBuilder);
    }
}
